package mx.com.mindbits.bandpay.adapters;

import java.text.SimpleDateFormat;
import java.util.List;

import mx.com.mindbits.bandpay.R;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.bandpay.utils.Utils;
import mx.com.mindbits.dao.transactions;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TransactionItemAdapter extends ArrayAdapter<transactions>{
	private Context context;
	private int listTransId;
	private int tvDescripcionId;
	private int tvFechaId;
	private int tvHoraId; 
	private int tvEstatus;
	private SimpleDateFormat simpleDateFormat;
	
	public int getListTransId() {
		return listTransId;
	}

	public void setListTransId(int listTransId) {
		this.listTransId = listTransId;
	}

	public int getTvDescripcionId() {
		return tvDescripcionId;
	}

	public void setTvDescripcionId(int tvDescripcionId) {
		this.tvDescripcionId = tvDescripcionId;
	}

	public int getTvFechaId() {
		return tvFechaId;
	}

	public void setTvFechaId(int tvFechaId) {
		this.tvFechaId = tvFechaId;
	}

	public int getTvHoraId() {
		return tvHoraId;
	}

	public void setTvHoraId(int tvHoraId) {
		this.tvHoraId = tvHoraId;
	}

	public int getTvEstatus() {
		return tvEstatus;
	}

	public void setTvEstatus(int tvEstatus) {
		this.tvEstatus = tvEstatus;
	}

	public TransactionItemAdapter(Context context, int resource, 
			List<transactions> items, int tvDescripcionId,
			int tvFechaId, int tvHoraId, int tvStatusId) {
		super(context, resource, items);
		Log.d(ConstantsValues.TAG_TRANS_HISTORY, "Inicio constructor TransactionItemAdapter");
		
		this.context = context;
		setListTransId( resource );
		setTvDescripcionId( tvDescripcionId);
		setTvFechaId( tvFechaId );
		setTvHoraId( tvHoraId );
		setTvEstatus( tvStatusId );
		simpleDateFormat = new SimpleDateFormat(ConstantsValues.FORMATO_FECHA_ES);
		Log.d(ConstantsValues.TAG_TRANS_HISTORY, "Fin constructor  TransactionItemAdapter");
	}
	
	@Override 
	public View getView(int position, View convertView, ViewGroup parent){
		Log.d(ConstantsValues.TAG_TRANS_HISTORY, "Inicio construccion de un view");
		TransactionViewHolder transViewHolder = null; 
        transactions transaction = getItem(position);
        
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
        	convertView = mInflater.inflate(listTransId, null);
        	transViewHolder = new TransactionViewHolder();
        	transViewHolder.imgMapa = (ImageView) convertView.findViewById(android.R.drawable.ic_menu_mapmode);
        	transViewHolder.tvDescripcion = (TextView) convertView.findViewById(tvDescripcionId);
        	transViewHolder.tvFecha = (TextView) convertView.findViewById(tvFechaId);
        	transViewHolder.tvHora =(TextView) convertView.findViewById(tvHoraId);
        	transViewHolder.tvEstatus = (TextView) convertView.findViewById(tvEstatus);
        	convertView.setTag(transViewHolder);
        }else{
        	transViewHolder = (TransactionViewHolder) convertView.getTag();
        }
        transViewHolder.tvDescripcion.setText(Utils.getCompanyLabel(String.valueOf(transaction.getProveedor())));
        transViewHolder.tvFecha.setText(simpleDateFormat.format(transaction.getFecha()));
        transViewHolder.tvHora.setText(transaction.getHora());
        Log.i(ConstantsValues.TAG_DAO, "FEcha " + transaction.getFecha() +":"+
        transaction.getHora() + "valor estatus [" + transaction.getEstatus()  +"| "
        		+ ConstantsValues.STATUS_PROCESADO +" ]" );
        int idEstadoString = (transaction.getEstatus() == ConstantsValues.STATUS_PROCESADO ) ? R.string.estatus_g : R.string.estatus_b;
		transViewHolder.tvEstatus.setText("Estado: " + context.getResources().getString(idEstadoString));
        Log.d(ConstantsValues.TAG_TRANS_HISTORY, "Fin construccion de un view");
		return convertView;
	}
	
}
